#!/bin/sh
npath="/dev/nvme"
logout="./check_test_main.log"
flitword='Celsius'

make

# rm -rf ${logout}
echo "" >> ${logout}
echo "------------------------------------------------------>" >> ${logout}
date >> ${logout}
#----------------------获取nvme盘的数量-----------------------
count=0
for ((i = 0; i < 30; i++)); do
    numStr=$(ls ${npath}${i})
    if [[ "${numStr}" == "${npath}${i}" ]]; then
        $((count++))
    fi
done

#----------------------产生随机数-----------------------------
function rand() {
    min=$1
    max=$(($2 - $min + 1))
    num=$(($RANDOM + 1000000000)) 
    echo $(($num % $max + $min))
}

function getTempIndex() {
#    echo $1
   
   if (( $1 < 50 )); then
       echo 0
   fi

   if (( 50 <= $1 && $1 < 70 )); then
       echo 1
   fi
   
   if (( $1 >= 70 && $1 < 80 )); then
       echo 2
   fi

   if (( $1 >= 80 && $1 < 85 )); then
       echo 3
   fi

   if (( $1 >= 85 )); then
       echo 4
   fi
}


#----------------------检测固态盘温度-----------------------------
while true; do
    res_str=""
    tempArry=(0 0 0 0 0)
    for ((i = 0; i < count; i++)); do
        res=$(./iSMART_64 -d ${npath}${i} | grep ${flitword} | sed -e "s/[[:space:]]//g")
        res="${res#*:}"
        # res="$(rand 40 90)"
        index=$(getTempIndex ${res})
        tempArry[$index]=$((${tempArry[$index]}+1))
        if [[ -n "${res}" ]]; then
            if ((i == 0)); then
                res_str=$(date +%H:%M:%S),$res
                continue
            else
                res_str=$res_str,$res
            fi
        fi
    done
    echo $res_str >>$logout
    # echo NVMETEMP:${tempArry[*]} >>$logout
   
    sleep 3s
done